import 'dart:convert';
import 'package:lib_core/lib_core.dart';
import 'package:uuid/uuid.dart';
import '../../const/net_cst.dart';

class OptionInterceptor extends QueuedInterceptorsWrapper {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
    String baseUrl = options.baseUrl;
    final env = await NetCst().getEnv();
    if (NetCst().hostUrlArray.contains(baseUrl)) {
      options.baseUrl = await NetCst().getHostUrlByEnv(env: env);
    }

    /*String? token = AuthService.to.accessToken;
    if (token != null) {
      options.headers['xtoken'] = token;

      final timestamp = DateTime.now().millisecondsSinceEpoch;
      const version = '1.0.0';
      final sign = _hostSign(options, timestamp, version, env);

      options.headers['appKey'] = await NetCst().getHostAppKeyByEnv(env: env);
      options.headers['sign'] = await sign;
      options.headers['timestamp'] = timestamp.toString();
      options.headers['version'] = version;

      /// 这个逻辑只在授权登录确认的接口上用，其他不用，后端没法修改，只能 App 去改了
      if (options.headers.containsKey('tempToken')) {
        options.headers['xtoken'] = options.headers['tempToken'];
      }

    }

    options.headers['traceId'] = const Uuid().v4();*/
    super.onRequest(options, handler);
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    super.onResponse(response, handler);
  }

  /*Future<String> _hostSign(RequestOptions options, int timestamp, String version, Env env) async {
    var path = options.path;
    if (!path.startsWith('/')) {
      path = '/$path';
    } else if (path.startsWith('//')) {
      path = path.substring(1, path.length);
    }

    final map = {
      'path': path,
      'timestamp': timestamp,
      'version': version,
      // if (options.data is Map) ...options.data,
    };

    final mapList = map.entries.map((e) => '${e.key}${e.value}').toList()..sort();
    final sign = '${mapList.join('')}${await NetCst().getHostSecretKeyByEnv(env: env)}';
    return md5.convert(utf8.encode(sign)).toString().toUpperCase();
  }*/
}
